package com.kitty.game.pet.handler.upgrade;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.message.RespOperateResult;
import com.kitty.game.pet.message.RespMorphSuccess;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 宠物幻化
 */
@Component
public class PetMorphHandler extends UpgradePetHandler {
    private static List<String> CAN_MORPH_NAMES = Arrays.asList("百年黑熊", "血幻豪猪", "赤血幼猿", "魅影毒蝎");
    private static String SUCCESS_ONE = "恭喜你，你的#Y{0}#n成功吸收了#Y{1}#n的真元，距离成功幻化#R{2}#n成长又近了一步！";
    private static String SUCCESS = "恭喜你，你成功地对#Y{0}#n的#R{1}#n进行了幻化。";

    @Override
    public String acceptUpgradePet(Role role, Pet pet, String ids, String itemPos, String costType, String otherPetPos) {
        String fieldName = null;
        String typeName = null;
        switch (costType) {
            case "1":
                fieldName = "life";
                typeName = "血量";
                break;
            case "2":
                fieldName = "mana";
                typeName = "法力";
                break;
            case "3":
                fieldName = "speed";
                typeName = "速度";
                break;
            case "4":
                fieldName = "phy";
                typeName = "物攻";
                break;
            case "5":
                fieldName = "mag";
                typeName = "法攻";
                break;
        }
        if (fieldName == null) {
            return "请勾选所要幻化的成长。";
        }
        if (pet.getType() == 1) {
            return "野生宠物不能进行宠物幻化。";
        }
        Pet otherPet = petService.getPetByPos(Byte.parseByte(otherPetPos), role);
        if (otherPet == null || !CAN_MORPH_NAMES.contains(otherPet.getName())) {
            return "只有百年黑熊、血幻豪猪、赤血幼猿、魅影毒蝎可作为副宠。";
        }
        if (otherPet.getEnchant() != 0 || otherPet.getEclosion() != 0) {
            return "只有后缀为#R宝宝#n的副宠可用于宠物幻化。";
        }
        if (!petService.isFullEffect(otherPet)) {
            return "只有基础成长全满的副宠才可用于宠物幻化。";
        }
        if (role.getPetBox().getFightPetId() == otherPet.getId()) {
            return "当前副宠正处于参战状态，无法用于宠物幻化。";
        }
        if (role.getPetBox().getSupplyPetId() == otherPet.getId()) {
            return "当前副宠正处于掠阵状态，无法用于宠物幻化。";
        }
        HashMap<String, Integer> morphEffect = pet.getMorphEffect();
        /**幻化的次数 最大只能有3次 先判断*/
        int currTimes = morphEffect.getOrDefault(fieldName + "_times", 0);
        if (currTimes >= 3) {
            return "你的#Y" + pet.getName() + "#n的#R" + typeName + "#n已经幻化过3次了，不能再进行幻化了。";
        }

        String result;
        petService.delPet(role, otherPet);
        int currState = morphEffect.getOrDefault(fieldName + "_stat", 0);
        int currValue = morphEffect.getOrDefault(fieldName, 0);
        morphEffect.put(fieldName + "_stat", currState + 1);
        int morphAddValue = addMorphValue(pet, fieldName);
        if (morphAddValue > 0) {
            morphEffect.put(fieldName + "_times", currTimes + 1);//幻化次数加1
            morphEffect.put(fieldName + "_stat", 0);//置零
            result = MessageFormat.format(SUCCESS, pet.getName(), typeName);
            pushOperTypeResult(role);
        } else {
            result = MessageFormat.format(SUCCESS_ONE, pet.getName(), otherPet.getName(), typeName);
        }
        roleService.addAchieveScore(role,"201053");
        morphEffect.put(fieldName, currValue + morphAddValue);
        petService.refreshPetInfo(role, pet);
        refreshPetField(role, pet);
        MessagePusher.pushMessage(role, new RespMorphSuccess());
        return result;
    }

    private void pushOperTypeResult(Role role){
        RespOperateResult operateResult = new RespOperateResult();
        operateResult.setSuccess(true);
        operateResult.setType("equip_upgrade");
        MessagePusher.pushMessage(role, operateResult);
    }
}
